package scct.hys.dao;

import scct.hys.model.Screening;
import scct.hys.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ScreeningDao {
    public static List<Screening> getAllScreenings() {

        List<Screening> list = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from cm_screening";
        PreparedStatement pstmt = null;
        try {
            pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String movieid=rs.getString("movieid");
                String starttime=rs.getString("starttime");
                String price=rs.getString("price");
                String type= rs.getString("type");

                list.add(new Screening(id,movieid,starttime,price,type));
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public boolean addScreening(  String movieid, String startTime, String price, String type){
//        System.out.println(name);
//        System.out.println(director);
//        System.out.println(actor);
//        System.out.println(releasedate);
//        System.out.println(duration);
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into cm_screening values(null,?,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(insert_sql);
            pstmt.setString(1,movieid);
            pstmt.setString(2,startTime);
            pstmt.setString(3,price);
            pstmt.setString(4,type);
            result=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return result>0;
    }
    public boolean updateScreening(int id, String movieid, String startTime, String price, String type){

        int count=0;
        Connection conn=DBUtils.getConnection();
        String update_sql="update cm_screening set movieid=?,startTime=?,price=?,type=? where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(update_sql);
            pstmt.setString(1,movieid);
            pstmt.setString(2,startTime);
            pstmt.setString(3,price);
            pstmt.setString(4,type);
            pstmt.setInt(5,id);

            count=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return count>0;
    }
    public boolean delScreening(int id) {
        System.out.println("id:"+id);
        int num=0;
        Connection conn=DBUtils.getConnection();
        String del_sql="delete from cm_screening where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(del_sql);
            pstmt.setInt(1,id);
            num=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return num>0;

    }
    public  Screening getScreeningById(int id){
        Screening screening=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from cm_screening where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                String movieid=rs.getString("movieid");
                String starttime=rs.getString("starttime");
                String price=rs.getString("price");
                String type= rs.getString("type");
                screening=new Screening(id,movieid,starttime,price,type);

            }
            DBUtils.closeConnection(conn);

        }catch (SQLException e){
            e.printStackTrace();
        }
        return screening;
    }}
